package com.balabala.gym_system.service.impl;

import com.balabala.api.VenuesFeignClient;

import com.balabala.dto.VenuesDto;

import com.balabala.goods.api.CourseFeignClient;
import com.balabala.goods.pojo.dto.BookCourseQueryDto;
import com.balabala.goods.pojo.vo.BookCourseDto;
import com.balabala.gym_system.mapper.AdminMapper;
import com.balabala.gym_system.mapper.CoachMapper;
import com.balabala.gym_system.mapper.ReservationListMapper;
import com.balabala.gym_system.pojo.dto.ReservationListDto;
import com.balabala.gym_system.pojo.entity.ReservationList;
import com.balabala.gym_system.pojo.vo.AdminVo;
import com.balabala.gym_system.pojo.vo.CoachVo;
import com.balabala.gym_system.pojo.vo.ReservationVo;
import com.balabala.gym_system.service.ReservationListService;
import com.balabala.result.Result;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
@Slf4j
public class ReservationListServiceImpl extends ServiceImpl<ReservationListMapper,ReservationList> implements ReservationListService {


    @Autowired
    private VenuesFeignClient venuesFeignClient;

    @Autowired
    private CourseFeignClient courseFeignClient;

    @Autowired
    private CoachMapper coachMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public List<ReservationVo> findByDto(ReservationListDto reservationListDto) {

        //预约表查找
        List<ReservationList> reservationListList = getBaseMapper().findByDto(reservationListDto);

        //新建ReservationVo的list
        List<ReservationVo> reservationVoList=new ArrayList<>();

        reservationListList.forEach(reservation->{
        log.info("传递的参数为:{},",reservation.getVenueId());

        //远程调用
        Result<VenuesDto> venuesById = venuesFeignClient.getVenuesById(reservation.getVenueId());
        ReservationVo reservationVo = new ReservationVo();
        //拷贝
        BeanUtils.copyProperties(reservation,reservationVo);
        reservationVo.setVenueName(venuesById.getData().getName());
        log.info("远程调用的数据为:{}",venuesById.getData().getName());

        reservationVoList.add(reservationVo);
        });
        return reservationVoList;
    }

    //根据预约课程id删除预约课程
    @Override
    public void deleteReservation(Long id) {
        getBaseMapper().deleteById(id);

    }

    //根据条件查找预约课程记录
    @Override
    public List<ReservationVo> findByQueryDto(BookCourseQueryDto bookCourseQueryDto) {

        //远程调用获得预约课程记录信息
        Result<List<BookCourseDto>> listByQueryDto = courseFeignClient.findListByQueryDto(bookCourseQueryDto);

        //新建Vo的list
        List<ReservationVo> reservationVoList=new ArrayList<>();

        listByQueryDto.getData().forEach(one->{

            //新建一个Vo对象
            ReservationVo reservationVo=new ReservationVo();

            //copy数据到vo
            BeanUtils.copyProperties(one,reservationVo);

            //根据教练id查询教练名称 预留TODO
            CoachVo coachVo = coachMapper.findById(one.getCoachId());
            if(coachVo!=null){
                //查找用户id
                AdminVo adminVo = adminMapper.findById(coachVo.getAdminId());

                //设置教练名称
                reservationVo.setCoachName(adminVo.getName());
            }



            //根据场馆id查询场馆名称
            Result<VenuesDto> venuesById = venuesFeignClient.getVenuesById(one.getVenuesId());
            //设置场馆名称到VO
            reservationVo.setVenueName(venuesById.getData().getName());

            //设置Vo的预约人数和上限人数 1-2
            reservationVo.setBookPeople(one.getNumAppointment()+"/"+one.getClassOrderNumber());

            //设置Vo的上课的时间区间 9:00-10:00
            String str= DateFormatUtils.format(one.getStartTime(), "yyyy-MM-dd HH:mm:SS");
            String[] split = str.split(":");
            String str1 = split[0]+":"+split[1];
            String end = DateFormatUtils.format(one.getEndTime(), "yyyy-MM-dd HH:mm:SS");
            String[] time = end.split(" ");
            String time1 = time[1];
            String[] time2 = time1.split(":");
            String time3=time2[0]+":"+time2[1];
            String time4=str1+"至"+time3;

            reservationVo.setDateRange(time4);
            System.out.println(time4);
            reservationVoList.add(reservationVo);
        });
        return reservationVoList;
    }
}
